package com.misyi.designpattern.state;

/**
 * 状态模式
 *
 * @author licong
 * @date 2021-09-14 下午5:52
 */
public class StatePatternDemo {

    public static void main(String[] args) {
        // 新建状态
        NewState newState = new NewState();

        // 状态发生变化
        Context context = new Context(newState);
        context.execute(1);
        context.execute(2);
        context.execute(3);

        // 适合场景:
        // 数据有状态, 状态就一定会流转, 从状态1变成状态2
        // 每个状态都有自己的执行逻辑, 有时候这个逻辑会非常复杂, 代码会非常多
        // 将每个状态都进行封装, 放到各自的状态类中去
        // 定义一个context 类, 用于流转状态, 并执行新状态的逻辑
    }


    public static class Context {
        private State state;

        public Context(State state) {
            this.state = state;
            this.state.execute();
        }

        public void execute(int stateType) {
            if (stateType == 1) {
                state = new ApprovingState();
                state.execute();
            } else if (stateType == 2) {
                state = new ApprovedState();
                state.execute();
            } else if (stateType == 3) {
                state = new FinishState();
                state.execute();
            }
        }

    }

    public interface State {
        void execute();
    }

    public static class NewState implements State {

        @Override
        public void execute() {
            System.out.println("执行销售出库单的新增状态的逻辑");
        }
    }

    public static class ApprovingState implements State {

        @Override
        public void execute() {
            System.out.println("执行销售出库单的审批状态的逻辑");
        }
    }

    public static class ApprovedState implements State {

        @Override
        public void execute() {
            System.out.println("执行销售出库单的已审批状态的逻辑");
        }
    }

    public static class FinishState implements State {

        @Override
        public void execute() {
            System.out.println("执行销售出库单的已完成状态的逻辑");
        }
    }
}
